/*
 * Copyright (c) 2011, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the [QTPRJ] nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL [QTPRJ] BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package joodin.impl.widgets.internal;

import joodin.impl.widgets.VaadinControl;

import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.spi.widgets.IToolBarButtonSpi;
import org.jowidgets.spi.widgets.IToolBarContainerItemSpi;
import org.jowidgets.spi.widgets.IToolBarItemSpi;
import org.jowidgets.spi.widgets.IToolBarPopupButtonSpi;
import org.jowidgets.spi.widgets.IToolBarSpi;
import org.jowidgets.spi.widgets.IToolBarToggleButtonSpi;

import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.Panel;
import com.vaadin.ui.themes.Runo;

public class ToolBarImpl extends VaadinControl implements IToolBarSpi {

	private final IGenericWidgetFactory factory;

	@SuppressWarnings("serial")
	public ToolBarImpl(final IGenericWidgetFactory factory, final Object parentUiReference) {
		super(new Panel(){
			@Override
			public String toString() {
				return "ToolBarImpl";
			}
		});
		getUiReference().setContent(new HorizontalLayout());
		getUiReference().addStyleName(Runo.PANEL_LIGHT);
		getUiReference().setScrollable(false);
		addButtonStyles();
		this.factory = factory;
		
	}

	@Override
	public Panel getUiReference() {
		return (Panel) super.getUiReference();
	}

	@Override
	public void remove(final int index) {
		//		final QAction item = getUiReference().getItem(index);
		//		if (item != null && !item.isDisposed()) { 
		//			item.dispose();
		//		}
	}

	@Override
	public IToolBarButtonSpi addToolBarButton(final Integer index) {
		ToolBarButtonImpl result = new ToolBarButtonImpl(new NativeButton());
		getUiReference().getContent().addComponent(result.getUiReference());
		return result;
	}

	@Override
	public IToolBarToggleButtonSpi addToolBarToggleButton(final Integer index) {
		ToolBarToggleButtonImpl result = new ToolBarToggleButtonImpl();
		getUiReference().getContent().addComponent(result.getUiReference());
		return result;
	}

	@Override
	public IToolBarPopupButtonSpi addToolBarPopupButton(final Integer index) {
		ToolBarPopupButtonImpl result = new ToolBarPopupButtonImpl();
		getUiReference().getContent().addComponent(result.getUiReference());
		return result;
	}

	@Override
	public IToolBarContainerItemSpi addToolBarContainer(final Integer index) {
		// TODO check if it works...
		final Panel composite = new Panel();
		composite.setCaption("Panel");
		composite.setWidth("16px");
		composite.setHeight("16px");
		
		return new ToolBarContainerItemImpl(composite, factory); 
	}

	@Override
	public IToolBarItemSpi addSeparator(final Integer index) {
		return new ToolBarItemImpl((new Label("<hr />",Label.CONTENT_XHTML)));
	}

	@Override
	public void pack() {
		
	}
	
	private void addButtonStyles(){
		String toolbar = ".toolbar .v-button {display: block; height: 55px; background: transparent; border: none; text-align: center;}" +
				".toolbar .v-button img { display: block; margin-left: auto; margin-right: auto; margin-bottom: 5px;}" +
				" .toolbar .v-button span { font-size: x-small;text-shadow: #fafafa 1px 1px 0;}" +
				".toolbar .v-button .v-button-wrap, .toolbar .v-disabled.v-button .v-button-wrap { background: transparent;" +
				" border: none; -webkit-border-radius: 0; -moz-border-radius: 0; border-radius: 0; -webkit-box-shadow: none;" +
				" -moz-box-shadow: none; box-shadow: none;} .toolbar .v-button:active .v-button-wrap, .toolbar .v-button.v-pressed " +
				".v-button-wrap { background: transparent; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; \n";
				
//		ApplicationWrapper.getCSSInjector().setValue(ApplicationWrapper.getCSSInjector().getValue() + toolbar);
	}
	
}
